﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.GoodInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.GoodInfo
{
    public class GsBasicInfoRepository : SqlServerRespository<GsBasicInfoEntity>, IGsBasicInfoRepository
    {
        public GsBasicInfoRepository()
        {
            Context = new WechatMallContext();
        }

        public IEnumerable<GsBasicInfoEntity> GetGsBasicInfoByGroupid(long groupid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsBasicInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsbasicinfo a
            INNER JOIN groupgsrelationship b ON a.gsid=b.gsid
            WHERE b.groupid=@groupid";
            var result = GetList(sql, new { groupid });
            return result;
        }

        public IEnumerable<GsBasicInfoEntity> GetAllgs()
        {
            return GetAll();
        }

        public void RemoveByGsid(long gsid, UnitOfWork unit = null)
        {
            DapperHelper.Execute($"delete from gsbasicinfo where gsid=@gsid", Context, unit, new { gsid });
        }

        public GsBasicInfoEntity GetGsEntityById(long gsid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsBasicInfoEntity>();
            var result = GetList($"SELECT {querysql}  FROM gsbasicinfo WHERE gsid=@gsid", new {gsid}).FirstOrDefault();
            return result;
        }

        public void AddGs(GsBasicInfoEntity entity, UnitOfWork unit = null)
        {
            Add(entity, unit);
        }

        public void EditGs(GsBasicInfoEntity entity, UnitOfWork unit = null)
        {
            Modify(entity, unit);
        }

        public void RemoveGs(GsBasicInfoEntity entity, UnitOfWork unit = null)
        {
            Remove(entity, unit);
        }

        public IEnumerable<GsBasicInfoEntity> GetGsBasicInfoByAppId(string appid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsBasicInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsbasicinfo a
            INNER JOIN appgsrelationship b ON a.gsid=b.gsid
            WHERE b.appid=@appid";
            var result = GetList(sql, new {appid});
            return result;
        }

        public IEnumerable<GsBasicInfoEntity> GetGsBasicInfoByShopid(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsBasicInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsbasicinfo a
            INNER JOIN shopgsrelationship b ON a.gsid=b.gsid
            WHERE b.shopid=@shopid";
            var result = GetList(sql, new {shopid});
            return result;
        }
    }
}